package com.bootdo.system.service.impl;

import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.WithdrawDao;
import com.bootdo.system.domain.WithdrawDO;
import com.bootdo.system.service.WithdrawService;



@Service
public class WithdrawServiceImpl implements WithdrawService {
	@Autowired
	private WithdrawDao withdrawDao;

	@Autowired
	private UserService userService;
	
	@Override
	public WithdrawDO get(Long id){
		return withdrawDao.get(id);
	}
	
	@Override
	public List<WithdrawDO> list(Map<String, Object> map){
		return withdrawDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return withdrawDao.count(map);
	}
	
	@Override
	public int save(WithdrawDO withdraw){
		//扣减
		UserDO userDO = userService.get(withdraw.getUserId());
		userDO.setBalance((Double.valueOf(userDO.getBalance())-withdraw.getNumber())+"");
		userService.update(userDO);
		return withdrawDao.save(withdraw);
	}
	
	@Override
	public int update(WithdrawDO withdraw){
		return withdrawDao.update(withdraw);
	}
	
	@Override
	public int remove(Long id){
		return withdrawDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return withdrawDao.batchRemove(ids);
	}

	@Override
	public boolean audit(Long id,Long userId) {
		WithdrawDO withdrawDO = withdrawDao.get(id);
		withdrawDO.setStatus(1);
		withdrawDO.setGrantTime(new Date());
		withdrawDO.setUpdatedBy(userId);
		withdrawDO.setUpdatedAt(new Date());
		withdrawDao.update(withdrawDO);
		return true;
	}

	@Override
	public boolean refuse(Long id, Long userId) {
		WithdrawDO withdrawDO = withdrawDao.get(id);
		withdrawDO.setStatus(2);
		withdrawDO.setGrantTime(new Date());
		withdrawDO.setUpdatedBy(userId);
		withdrawDO.setUpdatedAt(new Date());
		UserDO userDO = userService.get(withdrawDO.getUserId());
		userDO.setBalance((Double.valueOf(userDO.getBalance())+withdrawDO.getNumber())+"");
		withdrawDao.update(withdrawDO);
		userService.update(userDO);
		return true;
	}

}
